(function(){
	var api = 'http://h6.duchengjiu.top/shop/';
	
	//获取广告位编号
	var positionId = 0;
	(function(){
		//拿到轮播图容器
		var oBanner = document.getElementById("container");
		//请求banner数据
		$.get(api + 'api_position.php',{},function(result){
//			console.log(result);
			positionId = result.data[0].position_id;
			//在广告位编号里面再去请求具体广告位的数据
			$.get(api+ 'api_ad.php',{'position_id':positionId},function(result){
//				console.log(result);
				var obj=result.data;
//				console.log(obj);
//				var Ul = '<ul></ul>';
				var str = '';
				for(var a=0; a<4;a++){
					str += `
					<li><a href="javascript:;"><img src="${obj[a].url}" /></a></li>
					`;
//					console.log(str);
				};
//				console.log(str);
				oBanner.innerHTML = str;
				$('.container>ul').append($('.container>ul li:first').clone());
				var liWidth = $('.container ul li').outerWidth();
				var liNum = $(".container ul li").length;
				$('.container ul').css('width',liWidth*liNum);
				var n=0;
				function play(){
					n++;
					$('.container ul').animate({'left':-liWidth*n},1000,function(){
						if(n>3){
							n=0;
							$('.container ul').css('left',0);
						};
					});
					var b = n;
					if(b>3){b=0};
					$('.number span').eq(b).addClass("num-active").siblings().removeClass('num-active');
				};
				
				//自动播
				var timer;
				timer = setInterval(function(){
					play();
				},3000);
				
				//鼠标移入
				$('.lbtbox').mouseover(function(){
					clearInterval(timer);
				});
				
				//鼠标移出
				$('.lbtbox').mouseout(function(){
					timer = setInterval(function(){
						play();
					},3000);
				});
				
				//下一张
				$('.next').click(function(){
					if($('.container ul').is(':animated')){return};
					play();
				});
				//上一张
				$('#pre').click(function(){
					if($('.container ul').is(':animated')){return};
					n--;
//					console.log(n);
					if(n<0){
						n=3;
							$('.container ul').css('left',-liWidth*(n+1));
					};
					$('.container ul').animate({'left':-liWidth*n},1000,function(){});
					var b = n;
					if(b>3){n=0};
					$('.number span').eq(b).addClass("num-active").siblings().removeClass('num-active');
				});
				
				//num
				$('.number span').each(function(i){
					$(this).click(function(){
						n=i;
						$('.container ul').stop().animate({'left':-liWidth*i},2000);
						$(this).addClass("num-active").siblings().removeClass('num-active');
					});
				});
				
			})
		})
	})()
	
	//热门商品
	var oSplbzd = document.getElementById('splbzd');
	
	//console.log(oMored);
	var  templateStr= document.getElementById('splbzdx').innerHTML;
	//console.log(oSplbzd);
	//console.log(templateStr);
	var pages = 1;//当前页
	var pageCount;//总页数
	(function(){
		function product(a){
			$.get(api + 'api_goods.php',{'page':a,"pagesize":18},function(data){
			pageCount = data.page.page_count;
			var obj=data.data;
			console.log(data);
			var str = '';
			var tempile = _.template(templateStr);
			for(var i=0;i<obj.length;i++){
				str += tempile(obj[i])
			};
			oSplbzd.innerHTML += str;
			
			var aDl = document.getElementById('splbzd').getElementsByTagName('dl');
			//console.log(aDl)
			//console.log(obj);
			var b=0;
			for(var a=0;a<aDl.length;a++){
				aDl[a].style.margin = '20px 35px 0 0';
				b = a+1;
				if(b%3 == 0){
					aDl[a].style.margin = '20px 0 0 0';
				}
			};
			$('[lazyloadsrc]').YdxLazyLoad();
			});
			
		};
		product(pages);
		
		var oMored = document.getElementById('mored');
		var oMore = document.getElementById('more');
		oMored.onclick = function(){
			pages++;
			if(pages>pageCount){
				oMore.innerHTML = '没有更多了';
				return
			};
			product(pages);
		};
		
		
	})();
})();
